package ${controllerPkg};

import com.baomidou.mybatisplus.core.metadata.IPage;
import ${poPkg}.${poName};
import ${qoPkg}.${qoName};
import ${servicePkg}.${serviceName};
<#if swaggerEnable?? && swaggerEnable == 1>
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
</#if>
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.List;

@RestController
@RequestMapping("${alias}")
<#if swaggerEnable?? && swaggerEnable == 1>
@Api(tags = "${alias}")
</#if>
public class ${controllerName} extends BaseController {
    @Autowired
    private ${serviceName} ${alias}Service;

    <#if swaggerEnable?? && swaggerEnable == 1>
    @ApiOperation(value = "保存${alias}", httpMethod = "POST", notes = "保存到数据库。如果传入的对象有主键，则更新对象；没有主键则新增对象")
    @ApiImplicitParam(name = "${alias}", value = "${alias}对象")
    <#else>
    /**
     * 保存到数据库。如果传入的对象有主键，则更新对象；没有主键则新增对象
     * @param ${alias} 保存的对象
     */
    </#if>
    @PostMapping("/save")
    public void save${aliasCapFirst}(@RequestBody @NotNull @Validated ${poName} ${alias}) {
        <#if primaryKey??>
        if (null == ${alias}.get${primaryKey?cap_first}()) {
            ${alias}Service.add${aliasCapFirst}(${alias});
        } else {
            ${alias}Service.update${aliasCapFirst}(${alias});
        }
        <#else>
        ${alias}Service.add${aliasCapFirst}(${alias});
        </#if>
    }

    <#if primaryKey?? && primaryKeyJavaType??>
    <#if swaggerEnable?? && swaggerEnable == 1>
    @ApiOperation(value = "单个删除", httpMethod = "POST", notes = "根据主键删除记录")
    @ApiImplicitParam(name = "${alias}Id", value = "${alias}对象主键")
    <#else>
    /**
     * 根据主键删除记录
     * @param ${alias}的主键
     */
    </#if>
    @PostMapping("/del/{${primaryKey}}")
    public void del(@PathVariable("${primaryKey}") ${primaryKeyJavaType} ${primaryKey}) {
        ${alias}Service.del${aliasCapFirst}(${primaryKey});
    }

    <#if swaggerEnable?? && swaggerEnable == 1>
    @ApiOperation(value = "获取${alias}对象", httpMethod = "GET", notes = "根据主键获取单个对象信息")
    @ApiImplicitParam(name = "${primaryKey}", value = "${alias}对象主键")
    <#else>
    /**
     * 根据主键获取单个对象信息
     * @param ${primaryKey} 主键
     */
    </#if>
    @GetMapping("/${alias}/{${primaryKey}}}")
    public ${poName} ${alias}(@PathVariable("${primaryKey}") ${primaryKeyJavaType} ${primaryKey}) {
        return ${alias}Service.get${aliasCapFirst}(${primaryKey});
    }
</#if>

    <#if swaggerEnable?? && swaggerEnable == 1>
    @ApiOperation(value = "分页查询", httpMethod = "GET", notes = "根据条件查询记录并分页")
    @ApiImplicitParam(name = "${alias}Qo", value = "${alias}查询对象")
    <#else>
    /**
     * 根据条件查询记录并分页
     * @param ${alias}Qo 查询对象
     */
    </#if>
    @GetMapping("/${alias}s")
    public IPage<${poName}> ${alias}s(${qoName} ${alias}Qo) {
        return ${alias}Service.get${aliasCapFirst}AndPage(${alias}Qo);
    }

    <#if swaggerEnable?? && swaggerEnable == 1>
    @ApiOperation(value = "不分页查询", httpMethod = "GET", notes = "根据条件查询记录不分页")
    @ApiImplicitParam(name = "${alias}Qo", value = "${alias}查询对象")
    <#else>
    /**
     * 根据条件查询记录不分页
     * @param ${alias}Qo 查询对象
     */
    </#if>
    @GetMapping("/${alias}List")
    public List<${poName}> ${alias}List(${qoName} ${alias}Qo) {
        return ${alias}Service.get${aliasCapFirst}s(${alias}Qo);
    }
}
